package com.itboye.jinpuyiyuan.activity.home.healthconsultation;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

public class HealthConsultationDetailActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;
    private int num=5;
    private TextView tableRightTv;//点赞数
    private BranchAddressPresenter presenter;
    private ImageView likeImg;//点赞

    private String uid;//用户uid
    private PopupWindow likePop;//点赞pop
    private TextView sureTv,cancelTv,contentTv;
    private int cancelStye=0;// 取消点赞(如果传0，则第2次提示已经点赞，如果传1，则第2次点赞会进行取消上一次点赞操作)
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_healthconsultationdetail);
        initViewData();
    }

    private void initViewData() {
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("文章");
        uid= (String) SPUtils.get(getApplication(),null, Const.UID,"");

        updatLike(false);
        presenter=new BranchAddressPresenter(this);


//        rightImg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable));

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
            //文章点赞
            case R.id.tableRightTv:
            case R.id.likeImg:
                if (SPUtils.get(getApplication(),uid,"article_id",false)==null){
                    //这边文章从来没有点过赞的 第一次点
                    // cancel  取消点赞(如果传0，则第2次提示已经点赞，如果传1，则第2次点赞会进行取消上一次点赞操作)
                    presenter.postLike("10","1055",cancelStye);

                }else if ((boolean)SPUtils.get(getApplication()
                        ,uid,"article_id",false)){//上一次点赞成功
                    cancelStye=1;
                    startLikePop();

                }else {//上一次取消点赞成功
                    cancelStye=0;
                    presenter.postLike("10","1055",cancelStye);

                }

                break;
            // 点赞pop 点击事件
            case R.id.sureTv:
                presenter.postLike("10","1055",cancelStye);

                break;
            case R.id.cancelTv:
               likePop.dismiss();
                break;
        }
    }

     //点赞pop
    private  void startLikePop(){
        View inflate = LayoutInflater.from(this).inflate(R.layout.popwind_likearticle, null);
        likePop=new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        likePop.setAnimationStyle(R.style.popupwindow_anim_style);
        sureTv=inflate.findViewById(R.id.sureTv);
        cancelTv=inflate.findViewById(R.id.cancelTv);
        contentTv=inflate.findViewById(R.id.contentTv);
        contentTv.setText("已经点过赞了,确定取消点赞");
        sureTv.setText("确定");
        cancelTv.setText("取消");
        likePop.setFocusable(true);
        inflate.setFocusable(true);//comment by danielinbiti,设置view能够接听事件，标注1
        inflate.setFocusableInTouchMode(true); //comment by danielinbiti,设置view能够接听事件 标注2
        sureTv.setOnClickListener(this);
        cancelTv.setOnClickListener(this);
        likePop.showAtLocation(inflate, Gravity.TOP,0,0);
    }
    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){

            if (resultEntity.getEventType()==BranchAddressPresenter.By_Post_like_success){

              if (cancelStye==0){
                  SPUtils.put(getApplication(),uid,"article_id",true);
                  updatLike(true);
//                  MAlert.alert("点赞成功");
              }else if (cancelStye==1){
                  SPUtils.put(getApplication(),uid,"article_id",false);
                  updatLike(false);
//                  MAlert.alert("取消点赞成功");
              }
              if (likePop!=null){
                  likePop.dismiss();
              }

            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_Post_like_fail){

                MAlert.alert(resultEntity.getMsg());
            }

        }

    }
    //改变点赞状态 true 点赞  false 取消或默认没有点赞
    public void updatLike(boolean isLike){

        if (isLike){
           likeImg.setImageDrawable(getResources().getDrawable(R.drawable.zanwenzhangxuanze));
           num++;
        }else {
            likeImg.setImageDrawable(getResources().getDrawable(R.drawable.zanwenzhang));
            num--;
        }
        tableRightTv.setText(num+"");
    }
}
